package com.itheima.service.impl;

import com.itheima.dao.*;
import com.itheima.pojo.Blog;
import com.itheima.pojo.BlogSort;
import com.itheima.pojo.Tag;
import com.itheima.service.IndexService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    private BlogSortMapper blogSortMapper;

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private WebVisitMapper webVisitMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CommentMapper commentMapper;


    /**
     * 获取一年内的文章贡献度
     * @return
     */
    @Override
    public Map getBlogContributeCount() {
        //获取现在的时间
        Date now = new Date();
        long nowTime = now.getTime();
        List<List> blogContributeCount= new ArrayList<>();
        List list2= new ArrayList<>();
        List<String> contributeDate = new ArrayList<>();
        for (int i = 0; i <= 365; i++) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date(nowTime);
            Long lastDayTime = nowTime-1000*60*60*24;
            Date lastDayDate = new Date(lastDayTime);
            String createTime = simpleDateFormat.format(date);
            String lastDay = simpleDateFormat.format(lastDayDate);
            if(i==0){
                Long nextDayTime = nowTime+1000*60*60*24;
                Date nextDayDate = new Date(nextDayTime);
                String nextDay = simpleDateFormat.format(nextDayDate);
                List<Blog> logByCreatTime = blogMapper.findLogByCreatTime(createTime,nextDay);
                //今天的时间
                SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                String nowDay = simpleDateFormat1.format(now);
                //存入到集合中
                contributeDate.add(nowDay);

                if(logByCreatTime==null||logByCreatTime.size()<=0){
                    List list = new ArrayList();
                    list.add(createTime);
                    list.add(0);
                    blogContributeCount.add(list);
                }else {
                    int size = logByCreatTime.size();
                    List list = new ArrayList();
                    list.add(createTime);
                    list.add(size);
                    blogContributeCount.add(list);
                }
            }

            List<Blog> logByCreateTime = blogMapper.findLogByCreateTime(lastDay,createTime);
            if(logByCreateTime==null||logByCreateTime.size()<=0){
                List list = new ArrayList();
                list.add(lastDay);
                list.add(0);
                blogContributeCount.add(list);
            }else {
                int size = logByCreateTime.size();
                List list = new ArrayList();
                list.add(lastDay);
                list.add(size);
                blogContributeCount.add(list);
            }

            nowTime-=1000*60*60*24;
        }
        //一年前的时间
        Date boforeOneYear = new Date(nowTime+1000*60*60*24);
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String boforeOneYearDate = simpleDateFormat1.format(boforeOneYear);
        //存入到集合中
        contributeDate.add(boforeOneYearDate);



        Map map = new HashMap();
        map.put("blogContributeCount",blogContributeCount);
        map.put("contributeDate",contributeDate);


        return map;

    }

    /**
     * 获取每个分类下文章数目
     * @return
     */
    @Override
    public List<Map> getBlogCountByBlogSort() {
        //获取博客分类的全部uid
        List<BlogSort> blogSorts = blogSortMapper.selectAll();
        List<Map> mapList = new ArrayList<>();
        for (BlogSort blogSort : blogSorts) {
            String blogSortUid = blogSort.getUid();
            //根据博客分类的uid查找博客的数量
            List<Blog> blogList = blogMapper.findBlogByBlogSortUid(blogSortUid);
            HashMap map = new HashMap();
            map.put("blogSortUid",blogSortUid);
            map.put("name",blogSort.getSortName());

            //判断获取的集合是否为空
            if (blogList==null||blogList.size()<=0){
                //封装博客数量
                map.put("value",0);
                mapList.add(map);
            }else {
                //封装博客数量
                map.put("value",blogList.size());
                mapList.add(map);
            }

        }
        //返回数据
        return mapList;
    }

    /**
     * 获取每个标签下文章数目
     * @return
     */
    @Override
    public List<Map> getBlogCountByTag() {
        //获取博客标签的全部uid
        List<Tag> tags = tagMapper.selectAll();
        List<Map> mapList = new ArrayList<>();
        for (Tag tag : tags) {
            String tagUid = tag.getUid();
            //根据博客标签的uid查找博客的数量
            List<Tag> tagList = blogMapper.findBlogByBlogTag(tagUid);
            HashMap map = new HashMap();
            map.put("tagUid",tagUid);
            map.put("name",tag.getContent());

            //判断获取的集合是否为空
            if (tagList==null||tagList.size()<=0){
                //封装博客数量
                map.put("value",0);
                mapList.add(map);
            }else {
                //封装博客数量
                map.put("value",tagList.size());
                mapList.add(map);
            }

        }
        //返回数据
        return mapList;
    }

    /**
     * 获取最近一周用户独立IP数和访问量
     * @return
     */
    @Override
    public Map getVisitByWeek() {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        String today = simpleDateFormat.format(date);

        //创建一个list集合存储最近七天的日期
        List<String> dateList = new ArrayList<>();
        for (int i = 6; i >= 1; i--) {
            Date beforeDate = DateUtils.getDate(today, -i);
            String formatDate = simpleDateFormat1.format(beforeDate);
            dateList.add(formatDate);
        }
        dateList.add(simpleDateFormat1.format(date));


        //集合存储最近七天每一天的用户访问量
        List list1 = new ArrayList();
        //集合存储每天的独立ip数量
        List list2 = new ArrayList();
        for (int i = 0; i < dateList.size()-1; i++) {
            int pvCount = webVisitMapper.findPVCount(dateList.get(i), dateList.get(i + 1));
            list1.add(pvCount);
            Integer uvCount = webVisitMapper.findUVCount(dateList.get(i), dateList.get(i + 1));
            System.out.println(uvCount);
           list2.add(uvCount);
        }
        //获取今天时间的第二天
        long time = date.getTime();
        Long nextDayTime = time+1000*60*60*24;
        Date nextDate = new Date(nextDayTime);
        String nextDay = simpleDateFormat1.format(nextDate);
        int pvCount = webVisitMapper.findPVCount(dateList.get(6), nextDay);
        Integer uvCount = webVisitMapper.findUVCount(dateList.get(6), nextDay);
        list1.add(pvCount);
        list2.add(uvCount);



        HashMap hashMap = new HashMap();
        hashMap.put("data",dateList);
        hashMap.put("pv",list1);
        hashMap.put("uv",list2);
        return hashMap;
    }

    /**
     *首页初始化数据
     * @return
     */
    public Map init(){
        //获取博客的数量
        int blogCount = blogMapper.findBlogCount();
        //获取访问的数量
        int visitCount = webVisitMapper.findVisitCount();   
        //获取用户的数量
        int userCount = userMapper.findUserCount(); 
        //获取评论的数量
        int commentCount = commentMapper.findCommentCount();

        //把数据保存到map中
        HashMap map = new HashMap();
        map.put("blogCount",blogCount);
        map.put("visitCount",visitCount);
        map.put("userCount",userCount);
        map.put("commentCount",commentCount);
        return map;

    }

}
